<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from '@/views/dashboard/mixins/resize'

require('echarts/theme/macarons') // echarts theme

export default {
  name: "LineComponent",
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },


  data(){
    return {
      chart: null,

      option: {
        title: {
          text: this.className
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#0b2a1c'
            }
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '1%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['6-29', '6-30', '7-1', '7-2', '7-3', '7-4', '7-5']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '运行功率',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            data: this.getRandomList()
          },
          {
            name: '电费',
            type: 'line',
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            data: this.getPrice()
          },
        ]
      },


    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');

      this.chart.setOption(this.option);
    },

    getRandomList() {
      return Array.from({length: 7}, () => Math.floor(Math.random() * (151 - 50)) + 100);
    },

    getPrice() {
      return Array.from({length: 7}, () => Math.floor(Math.random() * (51-20)) + 20);
    }
  }


}

</script>

<style scoped>

</style>
